package com.lagou;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSesssion.SqlSession;
import com.lagou.sqlSesssion.SqlSessionFactory;
import com.lagou.sqlSesssion.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author harmony
 * @description 测试类
 * @date 2020/12/7 21:43
 */
public class IPersistence_Test {

    @Test
    public  void  test() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
        SqlSession sqlSession = build.openSqlSession();
        User user = new User();
        user.setId(3);
        user.setName("张三");
//        User user1 = sqlSession.selectOne("user.selectOne", user);
//        System.out.println(user1);
//        List<User> users = sqlSession.selectList("user.selectList");
//        for (User user1 : users) {
//            System.out.println(user1);
//        }

        IUserDao userDao = sqlSession.getMapper(IUserDao.class);
        //代理对象调用接口中任意方法，都会执行 proxyInstance里的invoke方法
        List<User> userList = userDao.findAll();
        for (User user1 : userList) {
            System.out.println(user1);
        }
    }

    @Test
   public  void  test1() throws Exception {
       InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
       SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
       SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
       SqlSession sqlSession = build.openSqlSession();
       User user = new User();
       user.setId(2);
       user.setName("张三");
       IUserDao userDao = sqlSession.getMapper(IUserDao.class);
       //代理对象调用接口中任意方法，都会执行 proxyInstance里的invoke方法
        Integer update = userDao.updateById(user);
        System.out.println(update);
    }


   @Test
   public  void  test2() throws Exception {
       InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
       SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
       SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
       SqlSession sqlSession = build.openSqlSession();
       User user = new User();
       user.setId(3);
       user.setName("张三");
       IUserDao userDao = sqlSession.getMapper(IUserDao.class);
       //代理对象调用接口中任意方法，都会执行 proxyInstance里的invoke方法
        Integer update = userDao.insertUser(user);
        System.out.println(update);
        }

    @Test
   public  void  test3() throws Exception {
       InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
       SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
       SqlSessionFactory build = sqlSessionFactoryBuilder.build(resourceAsStream);
       SqlSession sqlSession = build.openSqlSession();
       IUserDao userDao = sqlSession.getMapper(IUserDao.class);
       //代理对象调用接口中任意方法，都会执行 proxyInstance里的invoke方法
        User user = new User();
        user.setId(3);
        Integer update = userDao.deleteById(3);
        System.out.println(update);
    }
}
